
from rest_framework import serializers
from answers.models import Label, Reply, Question
from articles.models import Article
from articles.serializers.article import DetailedInnerArticleSerializers
from users.models import User


class LabelSerializer(serializers.ModelSerializer):
    '''标签技能模型序列化'''
    class Meta:

        model = Label

        fields = ('id','label_name')


class NewQuestionSerializer(serializers.ModelSerializer):
    '''最新问题模型序列化'''
    labels = serializers.StringRelatedField(label='labels',many=True,read_only=True)

    class Meta:

        model = Question

        fields = ('id','createtime','labels','reply',
                  'replyname','replytime','title','unuseful_count',
                  'useful_count','user','visits')


class LabelQuestionsSerializer(serializers.ModelSerializer):
    '''标签技能问题嵌套序列化模型'''
    labels = serializers.StringRelatedField(read_only=True,many=True)
    class Meta:

        model = Question

        fields = ('id','createtime','labels','reply',
                  'replyname','replytime','title','unuseful_count','useful_count','user','visits')


class LabelArticlesUserSerializer(serializers.ModelSerializer):
    '''标签技能问题嵌套序列化模型'''
    articles = DetailedInnerArticleSerializers(read_only=True,many=True)
    class Meta:

        model = User

        fields = ('id','username','avatar','articles','fans')



class LabelArticlesSerializer(serializers.ModelSerializer):
    '''标签技能问题嵌套序列化模型'''
    user = LabelArticlesUserSerializer(read_only=True)

    collected = serializers.BooleanField(default=False)

    collected_users = serializers.PrimaryKeyRelatedField(read_only=True,many=True)

    image = serializers.CharField(read_only=True)

    class Meta:

        model = Article

        fields = ('id','title','content','createtime','user','collected_users',
                  'collected','image','visits')



class DetailedLabelSerializer(serializers.ModelSerializer):
    '''标签技能模型序列化'''

    questions = LabelQuestionsSerializer(read_only=True,many=True)

    articles = LabelArticlesSerializer(read_only=True,many=True)

    class Meta:

        model = Label

        fields = ('id','questions','articles','label_name','desc','baike_url','label_icon','users')